﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Youz.Common.Helper
{
    public class LoginData
    {
        public DataProvider _Provider;
        public LoginData(string xpath)
        {
            if (_Provider==null)
            {
                this._Provider = new DataProvider(xpath);
            }
        }
    }
    public class EntityLogin
    {
        public string Name { get; set; }
        public string Link { get; set; }
    }
    public class DataProvider : AbstractXMLData, XMLData
    {
        public DataProvider() { }
        public DataProvider(string xpath) { Xpath = xpath; }
    }
    public interface XMLData
    {
         DataTable GetData();
         DataSet ReadXML();
         void SaveXML(DataSet Dataset);
         List<EntityLogin> GetEntityList();
    }
    public abstract class AbstractXMLData
    {
        protected string Xpath { get;  set; }
        public DataSet ReadXML()
        {
            DataSet ds = new DataSet();
            if (!File.Exists(this.Xpath))
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Link", typeof(string));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add("http://likeitgrp.net/affiche/show-32.aspx", "K幣認購活動20日盛大開啟（2014-11-18）");
                dt.Rows.Add("http://likeitgrp.net/affiche/show-31.aspx", "南非極品黃鑽鑽戒產品視頻（2014-11-14）");
                dt.Rows.Add("http://likeitgrp.net/affiche/show-30.aspx", "LIKEIT首屆限時搶購活動紀實（2014-11-14）");
                DataSet ds1 = new DataSet();
                ds1.Tables.Add(dt);
                SaveXML(ds1);
                ReadXML();
            }
            ds.ReadXml(Xpath);
            return ds;
        }
        public List<EntityLogin> GetEntityList()
        {
            DataTable XmlTable = GetData();

            if (XmlTable != null)
            {
                List<EntityLogin> list = new List<EntityLogin>();
                foreach (DataRow item in XmlTable.Rows)
                {
                    EntityLogin entitylogin = new EntityLogin();
                    entitylogin.Name = item["Name"].ToString();
                    entitylogin.Link = item["Link"].ToString();
                    list.Add(entitylogin);
                }
                return list;
            }
            return null;
        }
        public void SaveXML(DataSet Dataset)
        {
            Dataset.WriteXml(Xpath);
        }
        public DataTable GetData()
        {
            return ReadXML().Tables[0];
        }
    }
}
